package org.elzat.healthboxgatewayapp.controller;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elzat.healthboxgatewayapp.util.ResultEntity;
import org.elzat.healthboxgatewayapp.service.HealthBoxUserModifyService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "health-box-api/v1/users",
        produces = "application/json; charset=utf-8")
@AllArgsConstructor
@Slf4j
public class UserLockController {

    private final HealthBoxUserModifyService modifyService;

    /**
     * 用户封禁、解封接口, 只为系统管理员开放, 不删除数据, 只对Entity的locked字段进行操作
     * 封禁接口: DELETE .../users
     * 解封接口: PUT .../users
     * 两个接口参数一致都为名为userId的路径变量(PathVariable)
     * 返回值: ResultEntity类对象, 成功返回200和相应反馈信息, 失败返回400和相应失败信息
     *          不包含任何返回data
     * */
    @DeleteMapping("/{userId}")
    public ResultEntity<Object> lockUser(@PathVariable Long userId) {
        log.info("Got request to lock the user with user id: " + userId);
        Boolean locked = modifyService.lockUserByUserId(userId);
        if (locked) {
            log.info("successfully locked the user with user id: " + userId);
            return ResultEntity.ok().msg("封锁用户成功").build();
        }
        log.info("operation failed on locking the user with user id: " + userId);
        return ResultEntity.failure().msg("封锁用户失败").build();
    }

    @PutMapping("/{userId}")
    public ResultEntity<Object> unlockUser(@PathVariable Long userId) {
        log.info("Got request to unlock the user with user id: " + userId);
        Boolean unlocked = modifyService.unlockUserByUserId(userId);
        if (unlocked) {
            log.info("successfully unlocked the user with user id: " + userId);
            return ResultEntity.ok().msg("解除封锁成功").build();
        }
        log.info("operation failed on unlocking the user with user id: " + userId);
        return ResultEntity.failure().msg("解除封锁失败").build();
    }
}
